package com.cxx.food.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxx.food.constant.BizType;
import com.cxx.food.dao.MomentDao;
import com.cxx.food.dto.MomentDto;
import com.cxx.food.entity.*;
import com.cxx.food.mapper.MomentImageMapper;
import com.cxx.food.mapper.MomentMapper;
import com.cxx.food.service.CommentService;
import com.cxx.food.service.LikeService;
import com.cxx.food.service.MomentService;
import com.cxx.framework.web.CustomException;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MomentServiceImpl implements MomentService {
    @Resource
    private MomentDao momentDao;

    @Resource
    private MomentMapper momentMapper;

    @Resource
    private LikeService likeService;

    @Resource
    private CommentService commentService;

    @Resource
    private MomentImageMapper momentImageMapper;

    @Override
    public Boolean addMoment(MomentDto momentDto) {
        Moment moment = new Moment();
        BeanUtils.copyProperties(momentDto, moment);
        momentMapper.insert(moment);

        insertMomentImage(moment.getId(), momentDto.getImageList());

        return Boolean.TRUE;
    }

    @Override
    public Boolean updateMoment(Long id, MomentDto momentDto) {
        if (momentMapper.selectById(id) == null) {
            throw new CustomException("该朋友圈不存在");
        }

        Moment moment = new Moment();
        BeanUtils.copyProperties(momentDto, moment);
        moment.setId(id);
        momentMapper.updateById(moment);

        deleteMomentImage(id);
        insertMomentImage(id, momentDto.getImageList());
        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteMoment(Long id) {
        deleteMomentImage(id);
        momentMapper.deleteById(id);
        return Boolean.TRUE;
    }

    @Override
    public List<MomentDto> queryMomentList() {
        // TODO 只能查询好友的朋友圈
        return momentDao.queryMomentList();
    }

    @Override
    public IPage<MomentDto> queryMomentByPage(int currentPage, int pageSize) {
        Page<MomentDto> page = new Page<>(currentPage, pageSize);
        return momentDao.queryMomentByPage(page);
    }

    @Override
    public List<MomentDto> queryMomentById(Long id) {
        return momentDao.queryMomentById(id);
    }

    @Override
    public Boolean addComment(Long momentId, String content) {
        if (momentMapper.selectById(momentId) == null) {
            throw new CustomException("该朋友圈不存在");
        }

        return commentService.addComment(BizType.MOMENT, momentId, content);
    }

    @Override
    public Boolean addLike(Long momentId) {
        if (momentMapper.selectById(momentId) == null) {
            throw new CustomException("该朋友圈不存在");
        }

        return likeService.addLike(BizType.MOMENT, momentId);
    }

    @Override
    public Boolean deleteLike(Long momentId) {
        return likeService.deleteLike(momentId);
    }

    private void deleteMomentImage(Long id) {
        momentImageMapper.delete(Wrappers.lambdaQuery(MomentImage.class).eq(MomentImage::getMomentId, id));
    }

    private void insertMomentImage(Long momentId, List<String> imageList) {
        if (!imageList.isEmpty()) {
            momentDao.insertMomentImage(momentId, imageList);
        }
    }
}
